import store from '@/store'

function checkPermission(el, binding) {
	const {
		value
	} = binding
	const btnRights = store.getters && store.getters.btnRights

	if (value) {
		if (value.length > 0) {
			const permissionBtnRight = value

			const hasPermission = btnRights.some(btnRight => {
				return permissionBtnRight == btnRight.name
			})

			if (!hasPermission) {
				el.parentNode && el.parentNode.removeChild(el)
			}
		}
	} else {
		throw new Error(`need btnRights! Like v-permission="'service-add'"`)
	}
}

export default {
	inserted(el, binding) {
		checkPermission(el, binding)
	},
	update(el, binding) {
		checkPermission(el, binding)
	}
}
